<template>
  <div class="content pending">
    <div class="formSearch">
      <el-form :inline="true" :model="query">
        <el-form-item label="优惠券名称">
          <el-input
            v-model="query.couponName"
            maxlength="20"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="优惠券类型">
          <el-select
            v-model="query.couponType"
            placeholder="请选择优惠券类型"
          >
            <el-option label="全部" value="null" />
            <el-option label="满减券" value="1" />
            <el-option label="折扣券" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="query.state" placeholder="请选择优惠券状态">
            <el-option label="全部" value="null" />
            <el-option label="未开始" value="0" />
            <el-option label="进行中" value="1" />
            <el-option label="已结束" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="query.dates"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="search">查询 </el-button>
          <el-button type="primary" plain @click="clear">重置 </el-button>
          <el-button type="primary" plain @click="addActivity">新增 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table
        v-loading="loading.table"
        :data="tableData"
        border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column label="渠道券名称" width="220">
          <template #default="scope">{{ scope.row.couponName }}</template>
        </el-table-column>
        <el-table-column label="渠道券类型" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.couponType === 1">满减券</span>
            <span v-if="scope.row.couponType === 2">折扣券</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="渠道券规则"
          show-overflow-tooltip
        />
        <el-table-column label="状态" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.state === 0">未开始</span>
            <span v-if="scope.row.state === 1">进行中</span>
            <span v-if="scope.row.state === 2">已结束</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="领券时间"
          :formatter="(row) => row.takeStart + ' - ' + row.takeEnd"
          show-overflow-tooltip
        />
        <el-table-column
          prop="content"
          label="用券时间"
          :formatter="(row) => row.effectiveStart + ' - ' + row.effectiveEnd"
          show-overflow-tooltip
        />
        <el-table-column
          prop="createTime"
          label="创建时间"
          show-overflow-tooltip
        />
        <el-table-column label="操作" show-overflow-tooltip>
          <template #default="scope">
            <div class="btnList">
              <el-button
                v-if="scope.row.state === 0"
                type="primary"
                link
                @click="editActivity(scope.row.shopCouponId)"
              >编辑
              </el-button>
              <el-button
                v-if="scope.row.state !== 0"
                type="primary"
                link
                @click="visitActivity(scope.row.shopCouponId)"
              >详情
              </el-button>
              <el-button
                v-if="scope.row.state !== 0"
                type="primary"
                link
                @click="showData(scope.row.shopCouponId)"
              >数据
              </el-button>
              <el-popconfirm
                v-if="scope.row.state === 0 || scope.row.state === 2"
                title="确定删除此券？"
                @confirm="delCouponFn(scope.row)"
              >
                <template #reference>
                  <el-button
                    class="delCls"
                    link
                    type="danger"
                  >删除</el-button>
                </template>
              </el-popconfirm>
              <el-popconfirm
                v-if="scope.row.state === 1"
                title="是否停止此活动？"
                @confirm="stopFn(scope.row.shopCouponId)"
              >
                <template #reference>
                  <el-button
                    class="delCls"
                    link
                    type="danger"
                  >停止</el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 新增活动 -->
    <Form
      ref="couponFormRef"
      @reset="reset"
    />
    <!-- 数据展示 -->
    <el-dialog
      v-model="visible.info"
      :close-on-click-modal="false"
      title="数据效果"
      width="50%"
      top="50px"
      class="group-dialog"
    >
      <!-- 表格 -->
      <div class="dataEffect">
        <h3>数据统计至昨日</h3>
        <div class="couponTit">优惠券名称：{{ infoData.couponName }}</div>
        <div class="couponTit">
          优惠券总数：{{
            infoData.couponTotal ? infoData.couponTotal : '0'
          }}
          张，已领取 {{ infoData.received ? infoData.received : '0' }} 张
        </div>
        <div class="dataListBox">
          <div class="dataItem">
            <span>¥{{ infoData.total }}</span>
            <p>用券成交总额</p>
          </div>
          <div class="dataItem">
            <span>¥{{ infoData.useMoney }}</span>
            <p>使用优惠券总额</p>
          </div>
          <div class="dataItem">
            <span>{{ infoData.count }}件</span>
            <p>购买商品总数</p>
          </div>
          <div class="dataItem">
            <span>￥{{ infoData.productAvgPrice }}</span>
            <p>商品用券平均额度</p>
          </div>
        </div>
        <div class="tabListInfo">使用该优惠券购买的商品</div>
        <div class="tableBox">
          <el-table
            :data="infoData.products"
            border
            height="150"
            :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
            tooltip-effect="dark"
            style="width: 100%"
          >
            <el-table-column
              prop="productName"
              label="商品名称"
              show-overflow-tooltip
            />
            <el-table-column
              prop="number"
              label="付款件数"
              show-overflow-tooltip
            />
            <el-table-column
              prop="users"
              label="付款人数"
              show-overflow-tooltip
            />
          </el-table>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { defineComponent, onBeforeMount, reactive, ref } from 'vue';
import { useActivityHooks } from '@/views/marketing/hooks/dialogHooks.js'

import Form from './form.vue'
import {
  getCoupon,
  delCoupon,
  getCouponData,
  stopCoupon,
} from '@/api/marketing'

defineComponent({
  name: 'ChannelManage'
})

// 优惠券表单组件实例
const couponFormRef = ref();

const {
  addActivity,
  editActivity,
  visitActivity,
  reset,
} = useActivityHooks({
  ref: couponFormRef,
  resetCallbck: getAll
})

let page = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})
let query = reactive({
  couponName: '', // 优惠券名称
  couponType: null, // 优惠券类型
  dates: [], // 创建时间数组
  startTime: '', // 创建时间开始时间
  endTime: '', // 创建时间结束时间
  state: null, // 优惠券状态
});

let tableData = ref([]);
const loading = reactive({
  table: false
})

// 初始化查询所有数据
function getAll () {
  query.type = 2
  loading.table = true
  getCoupon(Object.assign({}, page, query)).then(res => {
    page.total = res.data.total
    tableData.value = res.data.list
  }).finally(() => {
    loading.table = false
  })
}
//  查询
function search () {
  page.page = 1
  if (query.dates.length !== 0) {
    query.startTime = query.dates[0]
    query.endTime = query.dates[1]
  }
  getAll()
}
// 清除
function clear () {
  query.couponName = '' // 优惠券名称
  query.couponType = null // 优惠券类型
  query.dates = [] // 创建时间数组
  query.startTime = '' // 创建时间开始时间
  query.endTime = '' // 创建时间结束时间
  query.state = null // 优惠券状态
  getAll()
}
function handleSizeChange (val) {
  page.pageSize = val
  getAll()
}
function handleCurrentChange (val) {
  page.page = val
  getAll()
}
// 停止优惠券活动
function stopFn (id) {
  stopCoupon({ shopCouponId: id }).then((res) => {
    if (res.code === '') {
      ElMessage.success('停止成功')
      page.page = 1
      getAll()
    } else {
      ElMessage.error(res.message)
    }
  })
}

let visible = reactive({
  info: false,
})
// 商品使用数据
let infoData = ref({});


// 显示数据效果
function showData (id) {
  getCouponData({ shopCouponId: id }).then((res) => {
    if (res.code === '') {
      infoData.value = res.data
    } else {
      ElMessage.error(res.message)
    }
  })
  visible.info = true
}
// 删除优惠券
function delCouponFn (data) {
  delCoupon({ shopCouponId: data.shopCouponId }).then((res) => {
    if (res.code === '') {
      ElMessage.success('删除成功')
      page.page = 1
      getAll()
    } else {
      ElMessage.error(res.message)
    }
  })
}

onBeforeMount(() => {
  getAll()
})
</script>

<style lang="scss" scoped>
.pending {
  padding: 30px;
}

.fenye {
  margin-top: 20px;
}

.dataEffect {
  .couponTit {
    margin: 20px 0;
  }

  .dataListBox {
    display: flex;
    justify-content: center;
    margin: 30px 0;

    .dataItem {
      width: 220px;
      height: 120px;
      border-radius: 8px;
      border: 1px solid #999999;
      text-align: center;
      margin: 0 10px;

      span {
        display: block;
        margin-top: 35px;
      }
    }
  }

  .tabListInfo {
    margin: 20px 0;
  }
}
</style>

<style scoped>
:deep(.btnList) .delCls {
  margin-left: 10px;
}

:deep(.group-dialog) .el-dialog__headerbtn .el-dialog__close {
  color: #ffffff;
}
</style>
